Ext.define('Hr.controller.cardinfo.CardInfoController',{
    extend:'Ext.app.Controller',
    stores:['cardinfo.NameStore','cardinfo.CardStore','cardinfo.CardTimeStore','cardinfo.CardComboStore'],
    models:[],
    views:['cardinfo.NameGrid','cardinfo.ShowPanel','cardinfo.EditWindow','cardinfo.ExpireWindow'],
    init:function(){
        var id;
        var pid;
        var data;
        var type;
        var flag2;
        var name;
        var flag;
        var change;
        var photoName;
        this.control({
            //姓名面板
            'namegrid grid':{
                render:function(){
                    var store = Ext.getCmp('nameGrid').store;
                    store.on('beforeload', function (store, options) {
                        Ext.apply(store.proxy.extraParams, {"value":null});
                    });
                    store.load();
                    //检查有无过期人员
                    var window = Ext.create('Hr.view.cardinfo.ExpireWindow');
                    var winStore = Ext.getCmp('expireGrid').store;
                    winStore.load({
                        callback:function(){
                            if (winStore.getCount()>=1 ) {
		                        window.show()
		                   }
                        }
                    });
                },
                selectionchange:function(model, records,obj){
                    try{
                        pid = records[0].raw.id;
                        name = records[0].raw.name;
                        var store = Ext.getCmp('grid').store;
                        store.on('beforeload', function (store, options) {
	                        Ext.apply(store.proxy.extraParams, {"pid":pid,'element':null,'value':null});
	                    });
	                    store.load();
                        Ext.getCmp('show').setTitle("“"+name+"”的证书一览");
                    }catch(e){}
                    Ext.getCmp('show').setDisabled(false);
                }
            },
            'namegrid button[action = search]':{
                click:function(){
                    var store = Ext.getCmp('nameGrid').store;
                    var value = Ext.getCmp('nametext').value;
                    store.on('beforeload', function (store, options) {
                        Ext.apply(store.proxy.extraParams, {"value":value});
                    });
                    store.load();
                }
            },
            //证书面板
            'show grid':{
                selectionchange:function(model, records,obj){
                    data = records[0];
                    try{id = records[0].raw.id;}catch(e){}
                    Ext.getCmp('del').setDisabled(false);
                    Ext.getCmp('update').setDisabled(false);
                }
            },
            'show button[action = search]':{
                click:function(){
                    var name = Ext.getCmp('textfield').getValue();
                    var val = Ext.getCmp('combo').getValue();
                    var store = Ext.getCmp('grid').store;
                    store.on('beforeload', function (store, options) {
                        Ext.apply(store.proxy.extraParams, {'name':name,"val":val});
                    });
                    store.load();
                }
            },
            'show button[action = add]':{
                click:function(){
                    Ext.create("Hr.view.cardinfo.EditWindow").show();
                    flag = true;
                }            
            },
            'show button[action = update]':{
                click:function(){
                    Ext.create('Hr.view.cardinfo.EditWindow').show();
                    var form = Ext.getCmp('form').getForm();
                    form.loadRecord(data);
                    photoName = Ext.getCmp('photoName').value;
                    if (name!=null&&name!="") {
                        Ext.getCmp('image').setSrc('../upload/card/'+name);
                    }
                    flag = false;
                }
            },
            'show button[action = del]':{
                click:function(){
                    Ext.MessageBox.show({
                        title:'警告！',
                        msg:'<center>删除后数据无法恢复，<br/>确定删除？</center>',
                        buttons:Ext.Msg.YESNO,
                        icon:Ext.Msg.WARNING,
                        fn:function(is){
                            if (is=='yes') {
                                Ext.Ajax.request({
                                    url:'../cardInfoAction/del.do',
                                    params:{'id':id},
                                    success:function(){
                                        Ext.MessageBox.alert("提示","删除成功！");
                                        var store = Ext.getCmp('grid').store;
                                        store.remove(data);
                                        Ext.getCmp('del').setDisabled(true);
                                        Ext.getCmp('update').setDisabled(true);
                                    }
                                })
                            }
                        }
                    })
                }
            },
            
            //编辑面板
            'edit button[action = submit]':{
                 click:function(obj){
                    Ext.getCmp('pid').setValue(pid);
                    var form = Ext.getCmp('form').getForm();
                    var window  = obj.up('edit');
                    if(form.isValid()){
                        Ext.Ajax.request({
                            url:'../cardInfoAction/addOrUpdate.do',
                            params:form.getValues(true),
                            success:function(){
                                var store = Ext.getCmp('grid').store;
                                if(flag){
                                    store.load();
                                    Ext.MessageBox.show({
                                        title:'提示',
                                        msg:'添加成功！<br>继续添加？',
                                        buttons:Ext.Msg.YESNO,
                                        fn:function(is){
                                            if(is=='no'){
                                                window.close();
                                            }else{
                                                form.reset();
                                                flag2 = false;
                                                Ext.getCmp('image').setSrc('../image/defult_card.png');
                                            }
                                        }
                                    })
                                }else{
                                    Ext.MessageBox.alert("提示",'修改成功！');
                                    window.close();
                                    store.load();
                                }
                              Ext.getCmp('del').setDisabled(true);
                              Ext.getCmp('update').setDisabled(true);
                            },
                            failure:function(){
                                Ext.MessageBox.show({
                                    title:'错误',
                                    msg:'<center>服务器传来一项错误！</center>',
                                    buttons:Ext.Msg.OK,
                                    icon:Ext.Msg.ERROR
                                });
                            }
                        })
                    }
                   
                 }
            },
            'edit filefield[id = fileName]':{
                change:function(obj){
                    flag2 = false;
                    Ext.MessageBox.show({
                        title:'警告',
                        msg:'若文件已存在，该操作会替换原有文件，确定上传？',
                        buttons:Ext.Msg.YESNO,
                        icon:Ext.Msg.WARNING,
                        fn:function(is){
                            if (is == 'yes') {
                                type = obj.getValue();
			                    var form =Ext.getCmp('form').getForm();
			                    type = "."+type.split(".")[1];
			                    photoName = new Date().getTime();
			                    Ext.getCmp('photoName').setValue(photoName+type);
                                form.submit({
                                    url:'../cardInfoAction/upload.do',
                                    success:function(){
                                        Ext.MessageBox.alert("提示","上传成功");
                                        Ext.getCmp('image').setSrc('../upload/card/'+photoName+type);
                                        flag2 = true;
                                    },
                                    failure:function(arg,action){
                                        Ext.MessageBox.alert('提示','服务器报告一项错误，原因可能是：<br>'+action.result.msg);
                                    }
                                });
                            }
                        }
                    })
                }
            },
            'edit datefield[id = usefulTime]':{
                change:function(obj){
                   Ext.getCmp('usefulTimeEnd').reset();
                   Ext.getCmp('usefulTimeEnd').setMinValue(obj.getValue());
                }
            },
            'edit':{
                close:function(){
                    if (flag2) {
	                      Ext.Ajax.request({
	                        url:'../cardInfoAction/delfile.do',
	                        params:{"fileName":(photoName+type)}
	                    });  
                    }
                }
            }
        })
    }
});